#include "main.h"
#include "global.h"
#include "sensorless.h"
#include "motor.h"
#include "init.h"

const unsigned char TAB_RFling[2][6]={
{RISING,FALLING,RISING,FALLING,RISING,FALLING},
{FALLING,RISING,FALLING,RISING,FALLING,RISING}};

const unsigned char TAB_BEMFChannel[2][6]={
{3,2,1,3,2,1},
{3,2,1,3,2,1} 
};

#define STCount 20//过零检测连续有效次数

unsigned char StOk=0;//启动成功标志
unsigned int StCountComm=0;//过零检测计数
unsigned int NumCoun=0;
unsigned char FFlag=0;

//带载参数
unsigned char QDPwm=8;
unsigned int TimeAli=100;


unsigned char Sta=0;

unsigned char RisingFalling;

unsigned int BEMFConvertedValue; 

unsigned int MINOUT=PWM_PERIOD*0.10;

unsigned char Sensorless_START(void);


void ADCS_chuli(void);

void BTIM3_IRQHandler(void)
{
	if(BTIM_GetITStatus(CW_BTIM3, BTIM_IT_UPDATE))
  {  
    BTIM_ClearITPendingBit(CW_BTIM3, BTIM_IT_UPDATE);
		
		if(Sta==1)//退磁结束
		{
      Sta=2;
			BTIM_Cmd(CW_BTIM3, DISABLE);
		}
		else if(Sta==3&&StOk==1)
		{//换相时刻。			
	    BTIM_Cmd(CW_BTIM3, DISABLE);
			
			if(Dir==1)
			 {
					bHallStartStep1++;
					if(bHallStartStep1>=6)bHallStartStep1=0;
			 }
			 else 
				{
					if(bHallStartStep1==0)bHallStartStep1=5;
					else bHallStartStep1--;
				}
				
	      Commutation(bHallStartStep1,Motor_Start_F); 
			}
	}
}


void ADC_IRQHandler(void)
{
    /* USER CODE BEGIN */
  if (CW_ADC->ISR_f.EOS)
  {
    ADC_ClearITPendingAll();//清除AD所有中断标志
		
	  ADC_GetSqr3Result(&SampleData[0]); //
		ADC_GetSqr1Result(&SampleData[1]); //
		ADC_GetSqr2Result(&SampleData[2]); //
		ADC_GetSqr0Result(&SampleData[3]); //bemf  
		ADC_GetSqr4Result(&SampleData[4]); //电位器 
		ADC_GetSqr5Result(&SampleData[5]); //电压
		
		if(Motor_Start_F==1)
		  {
			if(bHallStartStep1==0||bHallStartStep1==5)
          {if(SampleData[1]<DIin)
					  {bb+=DIin-SampleData[1];bbc++;if(bbc>=30){bbc=0;sumb=bb;bb=0;}}
					}//AB
      else if(bHallStartStep1==1||bHallStartStep1==2)
          {	if(SampleData[2]<DIin)
					  {cc+=DIin-SampleData[2];ccc++;if(ccc>=30){ccc=0;sumc=cc;cc=0;}}
					}//AC
	    else if(bHallStartStep1==3||bHallStartStep1==4)
         {	if(SampleData[0]<DIin)
            {aa+=DIin-SampleData[0];aac++;if(aac>=30){aac=0;suma=aa;aa=0;}}
				 }//BA
			}
		else 
      {
			   bbc=0;sumb=bb;bb=0;ccc=0;sumc=cc;cc=0;aac=0;suma=aa;aa=0;
			}
		
			 
	    aavsp+=SampleData[4];//调速平均滤波累计项
		  aacvsp++;
		  if(aacvsp>=200)
		 {
			aacvsp=0;sumavsp=aavsp;aavsp=0;
		 }
		 
		 if(Motor_Start_F==0||ErrorCode!=0)return;
		if(Sta!=2)return;
		ADCS_chuli();    
	}
    /* USER CODE END */
}

void ADCS_chuli(void)
{	
	static unsigned char cou=0;
	unsigned char hx=0;
	unsigned int thre=0;	  

		BEMFConvertedValue =SampleData[3];//[TAB_BEMFChannel[Dir][bHallStartStep1]];
		RisingFalling=TAB_RFling[Dir][bHallStartStep1];	 
 				
//		if(Tonoroff==0) 	thre=120;  //OFF时刻采集。0.2V阀值248
//		else 		
			thre=SampleData[5]>>1;			
		
		if(RisingFalling==FALLING)//下降沿
		{			
			if(BEMFConvertedValue<thre)
			{
				cou++;
				if(cou>=2)
				{
					cou=0;	
				  Sta=3;					
					
					StCountComm++;//正确检测到第三相反电动势		
					FFlag=1;
					hx=1;
				}
			}
			else {cou=0;}
	  }
		else if(RisingFalling==RISING)
		{
			if(BEMFConvertedValue>thre)
			{
				cou++;
				if(cou>=2)
				{
					cou=0;	
					Sta=3;			
					
					StCountComm++;//正确检测到第三相反电动势		
					FFlag=1;
					hx=1;
				}
			}
			else {cou=0;}
	  }	
		
		if(StCountComm>=STCount&&StOk==0)//连续检测到固定数量的过零时，认为启动成功
		{
				StOk=1;
		}	
 
    if(StOk==1&&hx==1)
    {
			    hx=0;
					
					if(StepTime>2000)
					{ 					
						BTIM_SetAutoreload(CW_BTIM3,StepTime>>3);								
					  BTIM_SetCounter(CW_BTIM3,0);
						BTIM_Cmd(CW_BTIM3, ENABLE);
					}
					else
					{
							if(Dir==1)
						 {
								bHallStartStep1++;
								if(bHallStartStep1>=6)bHallStartStep1=0;
						 }
						 else 
							{
								if(bHallStartStep1==0)bHallStartStep1=5;
								else bHallStartStep1--;
							}
							
							Commutation(bHallStartStep1,Motor_Start_F); 							
					}
	  }			
  
}


unsigned char Sensorless_START(void)
{
	unsigned int Com_time=0;			
		
	 	BTIM_SetCounter(CW_BTIM2,0);
		
		Tonoroff=0; 
		CW_ATIM->CCR4=PWM_PERIOD-400;
	
		StOk=2;
		Sta=0;
			
		MINOUT=PWM_PERIOD*QDPwm/100; ////////////////////////
		
	  OutPwmValue=PWM_PERIOD;
	  OutPwmValue=OutPwmValue*QDPwm/100;
	  Motor_Start_F=1; 
	CW_BTIM2->CNT=20000;
		UPPWM();
	  Commutation(bHallStartStep1,Motor_Start_F); 
				
	  TimeCountTemp=0;
	  while(TimeCountTemp<20);
	  TimeCountTemp=0;
		
		Com_time=0;
		StCountComm=0;
	  FFlag=0;
		
	  //StepTime=10;
	  StOk=0;	
		do{						
			if(Dir==1) 
			{
					bHallStartStep1++;
					if(bHallStartStep1>=6)bHallStartStep1=0;
			}
			else 
				{
					if(bHallStartStep1==0)bHallStartStep1=5;
					else bHallStartStep1--;
				}
				
			if(FFlag==0)StCountComm=0;
			FFlag=0;
			if(StOk==0)	
			{
	       Commutation(bHallStartStep1,Motor_Start_F); 
			}				   
			
			TimeCountTemp=0;
			while(TimeCountTemp<10)
			{
				if((FFlag==1||StOk==1)&&TimeCountTemp>=1)break;
			}

			Com_time++;
			OutPwmValue+=5;
		  UPPWM();	  
	}while(StOk==0&&Com_time<60&&ErrorCode==0); 
		
			
		if(StOk==0)
	  {
			Motor_Start_F=0;
			Commutation(0,0); 
			return 0;			
		}
		TimeCountVI=0;
		
		if(ErrorCode!=0) 
			return ErrorCode;
		else	 
			return 1;
}

